#-*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = ['GuiTypedChooser']

from Qt4 import QtGui, QtCore

from traits.api import implements

from _pylot.qtgui.styles.policy import *
from _pylot.qtgui.tools import create_background_palette
from pyLot.core import (datum_name, to_qt_tt, to_xml, ENCODING, Datum,
                        pathbook)


from ..interfaces.i_typedchooser import ITypedChooser
from .impl_widget import Widget

class GuiTypedChooser(QtGui.QWidget, Widget):
  u"""
  - SIGNAL dataChanged [value]: this signal is sent when chooser value changes
  """

  implements(ITypedChooser)

  def __init__(self, datum):
    QtGui.QWidget.__init__(self)
    Widget.__init__(self)

    self._datum = datum
    self.valid = True
    # Disable value_list auto update to avoid to store useless value
    # For example, each time an user roll a spinbox
    self._datum.setUpdateMode(Datum.ModeDoNotUpdate)

    self._base_layout = QtGui.QHBoxLayout(self)
    self._base_layout.setContentsMargins(5, 0, 5, 0)

  def check(self, state, msg):
    if state is True :
      self.ok()
    elif state is False :
      self.error()
    elif state is None :
      self.doubt()
    self._data_widget.setToolTip(msg)

  def error(self, error=u'Value cannot be converted'):
    self.valid = False
    self._data_widget.error(error)

  def doubt(self):
    self.valid = None
    self._data_widget.doubt()

  def ok(self):
    self.valid = True
    self._data_widget.ok()

  def _createConnexions(self):
    self._datum.connect(u'valueChanged', self.onDatumValueChanged)
    self._datum.connect(u'defaultValueChanged', self.onDatumDefaultValueChanged)
    self._datum.connect(u'valueListChanged', self.onDatumValueListChanged)
    self._datum.connect(u'toolTipChanged', self.onDatumToolTipChanged)

  def _deleteConnexions(self):
    self._datum.disconnect(u'valueChanged', self.onDatumValueChanged)
    self._datum.disconnect(u'defaultValueChanged', self.onDatumDefaultValueChanged)
    self._datum.disconnect(u'valueListChanged', self.onDatumValueListChanged)
    self._datum.disconnect(u'toolTipChanged', self.onDatumToolTipChanged)

  def onDatumValueChanged(self, datum, old, new):
    raise NotImplementedError

  def onDatumDefaultValueChanged(self, datum, old, new):
    raise NotImplementedError

  def onDatumValueListChanged(self, datum, lst):
    raise NotImplementedError

  def onDatumToolTipChanged(self, datum, old, new):
    self._data_widget.setToolTip(new)
    self.setToolTip(new)

  def setEnabled(self, state):
    self._data_widget.setEnabled(state)

  def setDisabled(self, state):
    self._data_widget.setDisabled(state)

  def getDataWidget(self):
    u"""
    :returns: QWidget containing data (ie QLineEdit, QSpinBox, etc.)
    """
    return self._data_widget

  def getPostWidgets(self):
    u"""
    Other widgets placed after label widget and data widget.
    For example a button opening a file selector or color chooser.  
    
    :returns: list of QWidget
    """
    return []

  def getPreWidgets(self):
    return []

  def getWidgets(self):
    lst = []
    lst += self.getPreWidgets()
    lst += self.getDataWidget()
    lst += self.getPostWidgets()
    return lst

  def getDatum(self):
    return self._datum

  def value(self):
    return self._datum.value

  def getValue(self):
    return self._datum.value

  def setValue(self, value):
    self._data_widget.setValue(value)

  def closeEvent(self, event):
    print 'closing'
    event.accept()

if __name__ == '__main__' :
  pass
